package com.issac.messagequeue.mqserver.datacenter;

import com.issac.messagequeue.MessagequeueApplication;
import com.issac.messagequeue.mqserver.coreclass.Binding;
import com.issac.messagequeue.mqserver.coreclass.Exchange;
import com.issac.messagequeue.mqserver.coreclass.ExchangeType;
import com.issac.messagequeue.mqserver.coreclass.MSGQueue;
import com.issac.messagequeue.mqserver.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

/**
 * DataBaseManager类整合数据库操作
 */
@Slf4j
public class DataBaseManager {
    private MetaMapper metaMapper;

    // 初始化数据库
    public void init() {
        metaMapper = MessagequeueApplication.context.getBean(MetaMapper.class);
        if (!checkDBExists()) {
            log.info("[DataBaseManager] 数据库不存在，即将开始创建！");
            // 数据库不存在 —— 创建
            // 先创建data目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            // 创建表
            createTable();
            log.info("[DataBaseManager] 数据库创建完毕！");
            // 插入默认数据
            addDefaultData();
            log.info("[DataBaseManager] 数据库初始化完成！");
        } else {
            log.info("[DataBaseManager] 数据库已存在！");
        }
    }

    private void addDefaultData() {
        // 1. 构造交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        // 2. 创建
        metaMapper.addExchange(exchange);
        log.info("[DataBaseManager] 默认数据加入完成！");
    }

    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        log.info("[DataBaseManager] 创建表完成！");
    }

    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }

    public void deleteDB() {
        File file = new File("./data/meta.db");
        boolean DBResult = file.delete();
        if (DBResult) {
            log.info("[DataBaseManager] 删除数据库文件成功！");
            File dataDir = new File("./data");
            boolean dirResult = dataDir.delete();
            if (dirResult) {
                log.info("[DataBaseManager] 数据库目录删除成功！");
            } else {
                log.info("[DataBaseManager] 数据库目录删除失败！");
            }
        } else {
            log.info("[DataBaseManager] 删除数据库文件失败！");
        }
    }

    public void addExchange(Exchange exchange) {
        metaMapper.addExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> searchAllExchanges() {
        return metaMapper.searchAllExchanges();
    }

    public void addQueue(MSGQueue queue) {
        metaMapper.addQueue(queue);
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public List<MSGQueue> searchAllQueues() {
        return metaMapper.searchAllQueues();
    }

    public void addBinding(Binding binding) {
        metaMapper.addBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public List<Binding> searchAllBindings() {
        return metaMapper.searchAllBindings();
    }
}
